package com.yz.newspaper.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yz.newspaper.pojo.po.ContractSigning;
import com.yz.newspaper.pojo.result.Result;
import com.yz.newspaper.pojo.unitPo.PageBean;
import com.yz.newspaper.service.IContractSigningService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 合同签约 前端控制器
 *
 * @author author
 * @since 2024-05-04
 */
@RestController
@RequestMapping("/contract-signing")
@Slf4j
public class ContractSigningController {

    @Autowired
    private IContractSigningService contractSigningService;

    @GetMapping("/list")
    @ApiOperation("查询所有合同签约")
    public Result getList(String partyA, String username, Integer year, Integer month, Integer pageNum, Integer pageSize) {
        log.info("partyA:{},username:{},year:{},month:{}", partyA, username, year, month);
        PageBean pageBean = contractSigningService.getList(partyA, username, year, month, pageNum, pageSize);
        return Result.success(pageBean);
    }

    @GetMapping("/{userId}")
    @ApiOperation("根据用户id查询合同签约")
    public Result getOne(@PathVariable("userId") Integer userId) {
        LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<ContractSigning>().eq(ContractSigning::getUserId, userId);
        List list = contractSigningService.list(lambdaQueryWrapper);
        return Result.success(list);
    }

    @GetMapping("/cs/{id}")
    @ApiOperation("根据id查询合同签约")
    public Result getOne_id(@RequestParam("id") Integer id) {
        ContractSigning contractSigning = contractSigningService.getById(id);
        return Result.success(contractSigning);
    }

    @PostMapping
    @ApiOperation("新增合同签约")
    public Result add(@RequestBody ContractSigning contractSigning) {
        contractSigning.setEndTime(LocalDate.now());
        boolean save = contractSigningService.save(contractSigning);
        return Result.success(save);
    }

    @PutMapping
    @ApiOperation("修改合同签约")
    public Result update(@RequestBody ContractSigning contractSigning) {
        boolean update = contractSigningService.updateById(contractSigning);
        return Result.success(update);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除合同签约")
    public Result delete(@PathVariable("id") Integer id) {
        boolean remove = contractSigningService.removeById(id);
        return Result.success(remove);
    }
}
